package com.achievo.know.web.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.CategoryApplication;
import com.achievo.know.application.FrontApplication;
import com.achievo.know.domain.Category;
import com.achievo.know.domain.CategoryContent;
import com.achievo.know.pojo.JsonCategory;
import com.achievo.know.pojo.SearchResult;
import com.achievo.know.web.util.PageActionSupport;
import com.dayatang.querychannel.support.Page;
import com.redhat.redwork.coc.RedworkAction;

public class SubIndexAction extends PageActionSupport implements RedworkAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6163635488534573917L;
	private String code;
	private String jsonResult;
	private Map<String,List<SearchResult>> cateResult;
	private CategoryContent content;
	private List<Category> cateList;
	
	public String home(){
		List<Long> cs=null;
		if(!isSuperAdmin()){
			cs=getUserCateIds();
		}
		cates=categoryApplication.getCates(cs);
		subCate=categoryApplication.get(cateId);
		parCate=categoryApplication.getParent(subCate);
		int i=0;
		if(parCate!=null){
			Category temp=categoryApplication.getParent(parCate);
			while(temp!=null && i++<1){
				parCate=temp;
				temp=categoryApplication.getParent(parCate);
			}
		}else{
			parCate=subCate;
			//show desc
			content=categoryApplication.getDesc(parCate.getId());
			return "sub_desc";
		}
		cateResult=new HashMap<String, List<SearchResult>>();
		List<Category>  cc=subCate.getChildren();
		for(Category c:cc){
			List<Long> tempCateIds=categoryApplication.listAllIds(c.getId());
			List<Long> cateIds=new ArrayList<Long>();
			for(Long id:tempCateIds){
				if(cs!=null){
					if(cs.contains(id)){
						cateIds.add(id);
					}
				}else{
					cateIds.add(id);
				}
			}
			Page<SearchResult> rs=frontApplication.findByCate(orderType,resType,cateIds,1,5);
			cateResult.put(c.getId()+"", rs.getResult());
		}
		if(cc.size()<=0){
			List<Long> tempCateIds=categoryApplication.listAllIds(cateId);
			List<Long> cateIds=new ArrayList<Long>();
			for(Long id:tempCateIds){
				if(cs!=null){
					if(cs.contains(id)){
						cateIds.add(id);
					}
				}else{
					cateIds.add(id);
				}
			}
			sResult=frontApplication.findByCate(orderType,resType,cateIds,currentPage,pageSize);
			if(subCate!=null){
				cateList=new ArrayList<Category>();
				Category temp=new Category();
				temp.setId(subCate.getId());
				temp.setName(subCate.getName());
				cateList.add(temp);
				Category parent=subCate.getParent();
				while(parent!=null){
					Category temp1=new Category();
					temp1.setId(parent.getId());
					temp1.setName(parent.getName());
					cateList.add(temp1);
					parent=parent.getParent();
				}
			}
			return "sub_index";
		}
		return "sub_home";
	}
	
	public String index(){
		List<Long> cs=null;
		if(!isSuperAdmin()){
			cs=getUserCateIds();
		}
		cates=categoryApplication.getCates(cs);
		subCate=categoryApplication.get(cateId);
		
		parCate=categoryApplication.getParent(subCate);
		int i=0;
		if(parCate!=null){
			Category temp=categoryApplication.getParent(parCate);
			while(temp!=null && i++<1){
				parCate=temp;
				temp=categoryApplication.getParent(parCate);
			}
		}else{
			parCate=subCate;
		}
		List<Long> tempCateIds=categoryApplication.listAllIds(cateId);
		List<Long> cateIds=new ArrayList<Long>();
		for(Long id:tempCateIds){
			if(cs!=null){
				if(cs.contains(id)){
					cateIds.add(id);
				}
			}else{
				cateIds.add(id);
			}
		}
		sResult=frontApplication.findByCate(orderType,resType,cateIds,currentPage,pageSize);
		if(subCate!=null){
			cateList=new ArrayList<Category>();
			Category temp=new Category();
			temp.setId(subCate.getId());
			temp.setName(subCate.getName());
			cateList.add(temp);
			Category parent=subCate.getParent();
			while(parent!=null){
				Category temp1=new Category();
				temp1.setId(parent.getId());
				temp1.setName(parent.getName());
				cateList.add(temp1);
				parent=parent.getParent();
			}
		}
		return "sub_index";
	}
	
	
	@Override
	public String getParam() {
		return "cateId="+cateId+"&orderType="+orderType+"&resType="+resType;
	}


	@Autowired
	private CategoryApplication categoryApplication;
	@Autowired
	private FrontApplication frontApplication;
	private List<JsonCategory> cates;
	private Category subCate;
	private Category parCate;
	private Page<SearchResult> sResult;
	private Long cateId;
	private String orderType="1";
	private String resType="1";
	
	

	
	public String getOrderType() {
		return orderType;
	}
	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}
	public String getResType() {
		return resType;
	}
	public void setResType(String resType) {
		this.resType = resType;
	}
	public Category getSubCate() {
		return subCate;
	}
	public void setSubCate(Category subCate) {
		this.subCate = subCate;
	}
	public Long getCateId() {
		return cateId;
	}
	public void setCateId(Long cateId) {
		this.cateId = cateId;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public List<JsonCategory> getCates() {
		return cates;
	}
	public void setCates(List<JsonCategory> cates) {
		this.cates = cates;
	}
	public Page<SearchResult> getSResult() {
		return sResult;
	}
	public void setSResult(Page<SearchResult> result) {
		sResult = result;
	}
	public void setCategoryApplication(CategoryApplication categoryApplication) {
		this.categoryApplication = categoryApplication;
	}
	public void setFrontApplication(FrontApplication frontApplication) {
		this.frontApplication = frontApplication;
	}

	public Map<String, List<SearchResult>> getCateResult() {
		return cateResult;
	}

	public void setCateResult(Map<String, List<SearchResult>> cateResult) {
		this.cateResult = cateResult;
	}

	public Category getParCate() {
		return parCate;
	}

	public void setParCate(Category parCate) {
		this.parCate = parCate;
	}

	public CategoryContent getContent() {
		return content;
	}

	public void setContent(CategoryContent content) {
		this.content = content;
	}

	public List<Category> getCateList() {
		return cateList;
	}

	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	
	
}
